package com.sys.servlet.mgr.commodity;


import com.sys.entity.Commodity;
import com.sys.entity.Customer;
import com.sys.pojo.JsonResult;
import com.sys.service.CommodityService;
import com.sys.service.CustomerService;
import com.sys.utils.WriteJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/mgr/commodity/showById")
public class ShowByIdServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter writer = resp.getWriter();
        JsonResult jr = new JsonResult();

        // 获取名片的id
        String id = req.getParameter("id");
        if (id == null || "".equals(id)){
            jr.setCode(600);
            jr.setMsg("请传入id");
            WriteJson.write(resp,jr);
            return;
        }
        CommodityService service = new CommodityService();
        try {
            Commodity commodity = service.showById(id);
            jr.setCode(0);
            jr.setData(commodity);
        } catch (Exception e) {
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("操作发生错误");
        }
        WriteJson.write(resp, jr);
    }
}
